package com.reading.cms.controller.system;

import com.reading.cms.controller.BaseController;
import com.reading.common.entity.system.SysUser;
import com.reading.common.utils.JsonResult;
import com.reading.core.manager.system.ISystemUserManager;
import com.reading.core.utils.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 业务处理
 *
 * @author ruoyi
 */
@Slf4j
@Controller
@RequestMapping("/system/user/profile")
public class ProfileController extends BaseController {

    private String prefix = "system/user/profile";

    @Autowired
    private ISystemUserManager userManager;

    /**
     * 个人信息
     */
    @GetMapping()
    public String profile(Model model) {
        SysUser sysUser = getUser();
        String roleGroup = userManager.selectUserRoleGroup(sysUser.getUserId());
        model.addAttribute("user", sysUser);
        model.addAttribute("roleGroup", roleGroup);
        return prefix + "/profile";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password) {
        SysUser sysUser = getUser();
        String encrypt = PasswordUtil.encryptPassword(sysUser.getLoginName(),password, sysUser.getSalt());
        if (sysUser.getPassword().equals(encrypt)) {
            return true;
        }
        return false;
    }

    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Integer userId, Model model) {
        SysUser sysUser = userManager.selectUserById(userId);
        model.addAttribute("user", sysUser);
        return prefix + "/resetPwd";
    }

    @PostMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(SysUser sysUser) {
        int rows = userManager.resetUserPwd(sysUser);
        if (rows > 0) {
            setUser(userManager.selectUserById(sysUser.getUserId()));
            return success();
        }
        return error();
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Integer userId, Model model) {
        SysUser sysUser = userManager.selectUserById(userId);
        model.addAttribute("user", sysUser);
        return prefix + "/edit";
    }

    /**
     * 修改头像
     */
    @GetMapping("/avatar/{userId}")
    public String avatar(@PathVariable("userId") Integer userId, Model model) {
        SysUser sysUser = userManager.selectUserById(userId);
        model.addAttribute("user", sysUser);
        return prefix + "/avatar";
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    @ResponseBody
    public JsonResult update(SysUser sysUser) {
        if (userManager.updateUser(sysUser) > 0) {
            setUser(userManager.selectUserById(sysUser.getUserId()));
            return success();
        }
        return error();
    }

    /**
     * 保存头像
     */
    @PostMapping("/updateAvatar")
    @ResponseBody
    public JsonResult updateAvatar(SysUser sysUser, @RequestParam("avatarfile") MultipartFile file) {
        return error("暂不支持");
    }
}
